﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace YouTubeStormer
{
    public partial class Stats : UserControl
    {
        public class Values
        {
            public int Cycles { get; set; }
            public int NextCycle { get; set; }
            public int Logins { get; set; }
            public int LoginErrors { get; set; }
            public int Posted { get; set; }
            public int PostingErrors { get; set; }
            public int SpammedComments { get; set; }
            public int Skipped { get; set; }
            public int Votes { get; set; }
            public int VoteErrors { get; set; }
            public KeyValuePair<int, int> CandyRatio { get; set; }
            public int AlreadyVotedGlitches { get; set; }
            public List<int> CycleDurations = new List<int>();
        }

        Timer refreshTimer = new Timer();
        DateTime startUp = DateTime.Now;
        WebClient webClient = new WebClient();
        public Values Statistics = new Values();

        public Stats()
        {
            InitializeComponent();
            refreshTimer.Interval = 300;
            refreshTimer.Tick += refreshTimer_Tick;
            refreshTimer.Start();
        }

        public void SendStats(string logText)
        {
            string html = "";
            html += "<u><b>" + DateTime.Now.ToString() + "</b></u>" + Environment.NewLine;
            html += GetStatRaw(true) + Environment.NewLine;
            html += Environment.NewLine + "<b>Last Cycle Log:</b>";
            html += Environment.NewLine + logText;
            html = html.Replace("\n", "<br/>").Replace("\r", "");
            html = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(html));
            html = html.Replace("=", "");
            html = html.Replace("+", "-");
            html = html.Replace("/", "_");
            webClient.DownloadString("http://zone117.com/update.php?data=" + html);
        }

        void refreshTimer_Tick(object sender, EventArgs e)
        {
            richTextBoxMain.Text = GetStatRaw(false);
        }

        public string GetStatRaw(bool html = false)
        {
            int avgCycleDuration = Statistics.CycleDurations.Count > 0 ? (int)Statistics.CycleDurations.Average() : 0;
            int timerInterval = Properties.Settings.Default.TimerInterval;

            string raw = string.Join(Environment.NewLine, new string[]{
                "Running Time: " + new TimeSpan(0, 0, (int)DateTime.Now.Subtract(startUp).TotalSeconds),
                "Next Cycle: " + (html ? DateTime.Now.AddSeconds(timerInterval * 60).ToShortTimeString() : new TimeSpan(0, 0, Statistics.NextCycle).ToString()),
                "Cycles Completed: " + Statistics.Cycles,
                "Avg. Cycle Time: " + avgCycleDuration + "s",
                "Logins: " + Statistics.Logins,
                "Login Errors: " + Statistics.LoginErrors,
                "Posted: " + Statistics.Posted,
                "Posting Errors: " + Statistics.PostingErrors,
                "Spammed Comments: " + Statistics.SpammedComments,
                "Skipped: " + Statistics.Skipped,
                "Votes: " + Statistics.Votes,
                "VoteErrors: " + Statistics.VoteErrors,
                "Candy Ratio: " + Statistics.CandyRatio.Key + "/" + Statistics.CandyRatio.Value,
                "Voting Glitches: " + Statistics.AlreadyVotedGlitches
            });
            if (html)
            {
                List<string> newRaw = new List<string>();
                foreach (string line in raw.Split('\n'))
                {
                    newRaw.Add("<b>" + line.Insert(line.IndexOf(": "), "</b>"));
                }
                raw = string.Join(Environment.NewLine, newRaw.ToArray());
            }
            return raw;
        }
    }
}
